<template>
  <div>
    <el-dialog
      :title="formData.id ? '修改': '增加'"
      :visible="show"
      @close="closePopFn"
    >
      <el-form ref="From" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
            <el-option v-for="item in EmployeeSimple" :key="item.id" :label="item.username" :value="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" size="small" @click="addFn">确定</el-button>
        <el-button size="small">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { addDepartmentsAPI } from '@/api/departments'
import { getEmployeeSimple, getDepartDetail, updateDepartments } from '@/api/employees'
export default {
  props: {
    originalOrganizationchart: {
      type: Array,
      default: () => {
        return []
      }
    },
    show: {
      type: Boolean,
      default: false
    },
    department: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    // 编码查重校验
    const duplicateChecking = (rules, val, cd) => {
      let ifl
      if (this.formData.id) {
        ifl = this.originalOrganizationchart.some((item) => item.code === val) && val !== this.department.code
      } else {
        ifl = this.originalOrganizationchart.some((item) => item.code === val)
      }
      if (ifl) {
        cd(new Error('编码重复'))
      } else {
        cd()
      }
    }
    return {
      // 表单双向绑定
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          // 编码查重校验
          { validator: duplicateChecking, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      // 存储部门负责人下拉菜单
      EmployeeSimple: []
    }
  },
  created() {
    this.getEmployeeSimpleFn()
  },
  methods: {
    // 获取部门详情，准备回填数据
    async getDepartDetailFn(id) {
      const res = await getDepartDetail(id)
      console.log('这里是修改', res)
      this.formData = res
    },

    // 关闭弹窗
    closePopFn() {
      this.$emit('closeFn')
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 重置表单
      this.$refs.From.resetFields()
    },
    async getEmployeeSimpleFn() {
      const res = await getEmployeeSimple(this.formData)
      this.EmployeeSimple = res
    },
    // 增加部门
    addFn() {
      this.$refs.From.validate(async(val) => {
        if (val) {
          if (this.formData.id) {
            await updateDepartments(this.formData)
            this.$message.success('修改成功')
            console.log('修改位置')
          } else {
            await addDepartmentsAPI({ ...this.formData, pid: this.department?.id || '' })
            this.$message.success('增加成功')
          }

          this.closePopFn()
          //   重新获取部门结构
          this.$emit('addFm')
          console.log(1111111111111)
          // 为编辑部门做准备
          this.formData = {
            name: '',
            code: '',
            manager: '',
            introduce: ''
          }
          // 重置表单
          this.$refs.From.resetFields()
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
